package CtbuLoanTests;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static java.lang.Thread.sleep;

public class AutoTest {
    private WebDriver webDriver;

    // 初始化Chrome浏览器实例
    @BeforeEach
    void setUp() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        System.setProperty("webdriver.chrome.driver",
                "C:\\Program Files\\Google\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe"); // 设置chromedriver的路径
        webDriver = new ChromeDriver(options);
        webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); // 隐式等待
    }


   // 登录页面测试用例
   @ParameterizedTest
   @CsvSource({"http://localhost:80/,zhangsan,password1,http://localhost/layout"})
   void Login(String url, String username, String password,String expected_jump_url) throws InterruptedException {
       webDriver.get(url);
       // 输入用户名
       webDriver.findElement(By.xpath("/html/body/div/div/div/div[2]/div/form/div[1]/div[2]/div/div/input")).sendKeys(username);
       sleep(3000);
       // 输入密码
       webDriver.findElement(By.xpath("/html/body/div/div/div/div[2]/div/form/div[2]/div[2]/div/div/input")).sendKeys(password);
       sleep(3000);
       // 点击登录按钮
       webDriver.findElement(By.xpath
               ("/html/body/div/div/div/div[2]/div/form/div[5]/div[2]/button/span")).click();
        sleep(3000); // 等待页面加载
        String cur_url=webDriver.getCurrentUrl();
        if(cur_url.equals(expected_jump_url)){
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
        }
    }


    //查询产品信息
    @ParameterizedTest
    @CsvSource({"工商银行住房贷款,3.9"})
    void Select(String productName,String interestRate){
        webDriver.get("http://localhost/layout/loanProduct");
        // 等待输入框变得可交互
        WebDriverWait wait=new WebDriverWait(webDriver,10);//最多等待10s
        //点击模糊查询输入框
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/section/main/form/div[1]/div/div/div/input"))).sendKeys(productName);
        //点击精准查询输入框
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/section/main/form/div[2]/div/div/div/input"))).sendKeys(interestRate);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/form/div[3]/div/button/span")).click();
        //是否出来了查询结果即产品名称工商银行住房贷款是否出现
        webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[2]"));

    }




    //修改产品信息
    @ParameterizedTest
    @CsvSource({"3.5"}) // 将利率值修改为3.5
    void modifyInterestRate(String interestRate) throws InterruptedException {
        webDriver.get("http://localhost/layout/loanProduct");

        // 点击修改按钮（这里应该使用更稳定的选择器）
        WebElement modifyButton = webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[9]/div/button[1]/span")); // 替换为稳定的选择器
        modifyButton.click();

        // 等待利率输入框变得可交互
        WebDriverWait wait = new WebDriverWait(webDriver, 10); // 最多等待10秒
        WebElement interestInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[4]/div[2]/div/div/input")));

        // 修改利率
        interestInput.clear(); // 如果输入框中有默认值，先清除
        interestInput.sendKeys(interestRate);

        // 点击保存按钮（同样，使用更稳定的选择器）
        WebElement saveButton = webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/footer/div/button/span"));
        saveButton.click();

    }


    //增加产品信息
    @ParameterizedTest
    @CsvSource({"招商银行创业贷款,4.2,200000, 60,无需抵押，低利率，前三年优惠"})
    void Add(String productName,String interestRate,String maxLimit,Integer maxTerm,String otherAttributes){
        webDriver.get("http://localhost/layout/loanProduct");
        //点击增加按钮
        webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/button[1]/span")).click();
        // 等待输入框变得可交互,增加产品信息
        WebDriverWait wait = new WebDriverWait(webDriver, 10); // 最多等待10秒
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[1]/div[2]/div/div/input"))).sendKeys(productName);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[2]/div[2]/div/div/div[1]/div[2]"))).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div[3]/div/div/div[1]/ul/li[4]"))).click(); //选择商业贷款
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[3]/div[2]/div/div/div[1]/div[2]"))).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div[4]/div/div/div[1]/ul/li[8]"))).click(); //选择浦发银行
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[4]/div[2]/div/div/input"))).sendKeys(interestRate);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[5]/div[2]/div/div/input"))).sendKeys(maxLimit);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[6]/div[2]/div/div/input"))).sendKeys(String.valueOf(maxTerm));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/div/form/div[7]/div[2]/div/textarea"))).sendKeys(otherAttributes);

        // 点击保存按钮（同样，使用更稳定的选择器）
        WebElement saveButton = webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/div[3]/div/div/footer/div/button/span"));
        saveButton.click();

    }

    // 测试结束后关闭浏览器实例
    @AfterEach
    void tearDown() {
        if (webDriver != null) {
            webDriver.quit();
        }
    }

    //删除信息
    @Test
    void DeleteCtbuLoan() throws InterruptedException {
        //打开主页面
        webDriver.get("http://localhost/layout");
        sleep(6000);
        //点击贷款产品管理
        webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/aside/ul/li[2]/div/span")).click();
        sleep(6000);
        //点击产品信息
       webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/aside/ul/li[2]/ul/li[1]")).click();
        sleep(6000);
        //点击删除按钮
        webDriver.findElement(By.xpath("/html/body/div[1]/div/section/section/main/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[9]/div/button[2]/span")).click();

    }



        //退出登录
        @Test
        void LogOut() throws InterruptedException{
            //打开主页面
            webDriver.get("http://localhost/layout");
            //点击欢迎
            webDriver.findElement(By.cssSelector("#app > div > section > header > div.welcome > div")).click();
            sleep(2000);
            //点击退出登录按钮
            webDriver.findElement(By.cssSelector("body > div>div>div>div>div>ul> li:nth-child(3)")).click();
            sleep(2000);
            //检查是否存在用户名输入框以此来校验是否跳转到了登录页面
            webDriver.findElement(By.cssSelector("body > div>div>div>div>div>form>div:nth-child(1)>div:nth-child(2)>div>div>input"));
        }
}
